#!/usr/bin/perl -c

package MooseX::CLI::Command::Help;

=head1 NAME

MooseX::CLI::Command::Help - Help command for CLI apps

=head1 SYNOPSIS

  package MyApp::CLI::Command::Help;
  use Any::Moose;
  extends 'MyApp::CLI::Command';
  with 'MooseX::CLI::Command::Help';

  $ myapp help

=head1 DESCRIPTION

This role allows to implement C<help> command for CLI applications.

=cut

use 5.006;

use strict;
use warnings;

our $VERSION = '0.01';


# Debug if env is set
use if $ENV{PERL_DEBUG_MOOSEX_CLI_COMMAND}, 'Smart::Comments';

# Assertions
use Test::Assert ':assert';
use Exception::Assertion verbosity => 4;


use Any::Moose '::Role';

with 'MooseX::CLI::Command';


=head1 METHODS

=over

=item <<before>> run()

Implements C<help> command.  Loads command class which is pointed by
additional arguments for C<help> command and calls C<show_help> method with
this class as an argument.

=back

=cut

before 'run' => sub {
    my ( $self ) = @_;

    my @extra_argv = @{ $self->extra_argv };
    ### @extra_argv

    my $base = $self->_cmd_base_class;
    assert_not_null($base) if ASSERT;

    my $package = join '::', $base, map { ucfirst($_) } @extra_argv;
    ### $package

    if (not $self->_load_cmd_package($package)) {
        $package = $base;
    };

    $package = $self->_get_superclass_for_cmd_alias($package);

    return $self->show_help($package);
};

1;
